package com.example.pcbackend.api;

import com.auth0.jwt.algorithms.Algorithm;
import com.example.pcbackend.config.UserContext;
import com.example.pcbackend.dto.ScanQrcodeLoginConfirmDto;
import com.example.pcbackend.pojo.SysUser;
import com.example.pcbackend.service.ScanQrcodeLoginService;
import com.example.pcbackend.utils.JwtUtil;
import com.example.pcbackend.vo.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

/***
 * @title LoginController
 * @description <description class purpose>
 * @author WeiShuo
 * @version 1.0.0
 * @create 2023/10/4 20:23
 **/
@Controller
@RequestMapping("/api")
public class LoginController {

    @Autowired
    private ScanQrcodeLoginService scanQrcodeLoginService;


    /**
     * app登录，分配token
     */
    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResultJson login(@RequestBody SysUser user) {

        SysUser sysUser = new SysUser(user.getUsername(), null);
        String accessToken = createToken(sysUser);
        return ResultJson.success(accessToken, "登录成功");
    }

    /**
     * 认证 accessToken 是否有效；并且认证 临时token是否是有效二维码
     */
    @ResponseBody
    @RequestMapping(value = "/scanQrcodeLogin/authAccessToken", method = RequestMethod.POST)
    public ResultJson authAccessToken(String token) {
        scanQrcodeLoginService.authAccessToken(token);
        SysUser user = UserContext.getUser();
        return ResultJson.success(user ,"成功");
    }

    /**
     * 确认登录
     * @param token 临时扫码登录凭证
     * @param operationType 操作类型  confirm：确认 ；cancel：取消登录             、
     */
    @ResponseBody
    @RequestMapping(value = "/scanQrcodeLogin/confirm", method = RequestMethod.POST)
    public ResultJson confirm(@RequestBody ScanQrcodeLoginConfirmDto confirmDto) {

        scanQrcodeLoginService.confirm(confirmDto.getToken(), confirmDto.getOperationType());
        return ResultJson.success();
    }


    /**
     * 创建token
     * @param user
     * @return
     */
    private String createToken(SysUser user) {

        Algorithm algorithm = JwtUtil.hs256(null);
        Date nowTime = Calendar.getInstance().getTime();
        //失效日期
        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.MINUTE, 10);
        calendar.add(Calendar.DATE, 7);
        Date expTime = calendar.getTime();

        HashMap<String, String> claimMap = new HashMap<>();
        claimMap.put("username", user.getUsername());
        claimMap.put("type", "app");


        return JwtUtil.createToken("client", algorithm, nowTime, expTime, user.getUsername(), UUID.randomUUID().toString(), claimMap);
    }

}
